package com.lemon1.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import com.lemon1.Constants;

public class AssetsOperate {

	public static void goCopyAssets2Where(Context ct) {
		AssetManager assetManager = ct.getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("sms", e.getMessage());
		}
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			Constants.VXCOPYSRCPATH = "/sdcard/";
		}else{
			Constants.VXCOPYSRCPATH = "/data/data/com.iphonele.client/files/";
		}
		String strDestFile;
		int iFileCnt = files.length;
		for (int i = 0; i < iFileCnt; i++) {
			String strFileNameSrc = files[i];
			if (strFileNameSrc.contentEquals("images") || strFileNameSrc.contentEquals("sounds") || strFileNameSrc.contentEquals("skins") || strFileNameSrc.contentEquals("webkit")) {
				// dont copy.. not really a asset file
				continue;
			}
			strDestFile = Constants.VXCOPYSRCPATH + strFileNameSrc;
			VxCopyAsssetToSdcard(ct, strFileNameSrc, strDestFile, true);
		}
	}

	static int VxCopyAsssetToSdcard(Context ct, String strFileSrc, String strFileDest, boolean bDontCopyIfExistsAndSameSize) {
		int iSrcFileSize = 0;
		int iDestFileSize = 0;
		InputStream oInputStream = null;
		OutputStream oOutputStream = null;
		try {
			oInputStream = ct.getAssets().open(strFileSrc);
			if (null != oInputStream) {
				iSrcFileSize = oInputStream.available();
			} else {
				Log.e("UI", "ERROR: VxCopyResourceToSdcard could not open file " + strFileSrc);
				return -1;
			}
		} catch (IOException e) {
			Log.e("UI", "ERROR: VxCopyResourceToSdcard could not open file " + strFileSrc);
			e.printStackTrace();
			return -1;
		}
		if (bDontCopyIfExistsAndSameSize) {
			// get dest file size and stream
			try {
				InputStream oTmpStream = null;
				File readFile = new File(strFileDest);
				oTmpStream = new FileInputStream(readFile);
				;
				if (null != oTmpStream) {
					iDestFileSize = oTmpStream.available();
					oTmpStream.close();
				} else {
					Log.e("UI", "ERROR: VxCopyAsssetToSdcard could not get size of file " + strFileDest);
					return -1;
				}
			} catch (IOException e) {
				Log.e("UI", "VxCopyAsssetToSdcard dest file doesnt exist.. will copy " + strFileDest);
			}
		}
		if (bDontCopyIfExistsAndSameSize && (iSrcFileSize == iDestFileSize)) {
			Log.i("UI", "VxCopyAsssetToSdcard file exists " + strFileDest);
			return 0;
		}
		// Read the entire asset into a local byte buffer.
		byte[] buffer = new byte[iSrcFileSize];
		try {
			oInputStream.read(buffer);
			oInputStream.close();
		} catch (IOException e) {
			Log.e("UI", "ERROR: VxCopyAsssetToSdcard could not read file " + strFileSrc);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// write buffer to destination file
		try {
			File oWriteFile = new File(strFileDest);
			try {
				oWriteFile.createNewFile();
			} catch (IOException e) {
				Log.e("UI", "error creating file " + strFileDest, e);
				e.printStackTrace();
				return -1;
			}
			oOutputStream = new FileOutputStream(oWriteFile);
			// Don’t forget to close stream
			if (oOutputStream != null) {
				try {
					oOutputStream.write(buffer);
					oOutputStream.flush();
					oOutputStream.close();
					Log.i("UI", "Success copying file " + strFileDest + " size " + iSrcFileSize);
				} catch (IOException e) {
					Log.e("UI", "ERROR: VxCopyResourceToSdcard could not write file " + strFileDest);
					e.printStackTrace();
					return -1;
				}
			}
		} catch (IOException e) {
			Log.e("UI", "ERROR: VxCopyAsssetToSdcard could not read file " + strFileSrc);
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
